<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class map extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->load->library("pagination");
        $this->load->model('desa_model');
    }

    public function index() {
        $data["list_kec"] = $this->desa_model->get_all_kec();
        $this->load->view('map/header');
        $this->load->view('map/petadasar', $data);
        $this->load->view('map/footer');
    }

    public function kemiskinan() {
        $data["list_kec"] = $this->desa_model->get_all_kec();
        $this->load->view('map/header');
        $this->load->view('map/petadasar_miskin', $data);
        $this->load->view('map/footer');
    }
    
    public function pendidikan(){
        $data["list_kec"] = $this->desa_model->get_all_kec();
        $this->load->view('map/header');
        $this->load->view('map/petadasar_pendidikan', $data);
        $this->load->view('map/footer');
    }

    public function get_polygon_kab() {
        header('Access-Control-Allow-Origin: *');
        $desa_list = array();

        $p = $this->desa_model->get_layer_kabupaten();

        foreach ($p->result_array() as $r) {//data loc buat marker
            $data_way = explode(" ", $r["KAB_GEOMETRY"]);

            $point_desa_list = array();
            for ($i = 0; $i < count($data_way) - 1; $i++) {
//                    echo $data_way[$i]; echo " || ";
                $latlng = explode(",", $data_way[$i]);
                $lat = $latlng[0];
                $lng = $latlng[1];

//                    echo $latlng[0]; echo " | ";
//                    echo $lng = $latlng[1];echo " <br/> ";

                if ($i == 0) {//echo $latlng[1];
                    $temp1 = explode(">", $latlng[0]);
                    $lat = $temp1[4];
                }

                if ($i == count($data_way) - 1) {//echo $latlng[1];
                    $temp = explode("<", $latlng[1]);
                    $lng = $temp[0];
                }

                $data_latlng = array(
                    'LAT' => $lng,
                    'LNG' => $lat
                );

                array_push($point_desa_list, $data_latlng);
            }

            $data_per_desa = array(
                'KAB_KODE' => $r["KAB_ID"],
                'GEO' => $point_desa_list
            );
            array_push($desa_list, $data_per_desa);
        }

        echo json_encode(array('KAB' => $desa_list));
        exit;
    }

    public function get_polygon_desa($id_kec = "") {
        header('Access-Control-Allow-Origin: *');
        $desa_list = array();

        $p = $this->desa_model->get_desa_poly($id_kec);

        foreach ($p->result_array() as $r) {//data loc buat marker
            $data_way = explode(" ", $r["DESA_GEOMETRY"]);
            $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
            $color = '#' . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)];

            $point_desa_list = array();
            for ($i = 1; $i < count($data_way); $i++) {
                $latlng = explode(",", $data_way[$i]);
                $lat = $latlng[0];
                $lng = $latlng[1];

                if ($i == 0) {//echo $latlng[1];
                    $temp1 = explode(">", $latlng[0]);
                    $lat = $temp1[4];
                }

                if ($i == count($data_way) - 1) {//echo $latlng[1];
                    $temp = explode("<", $latlng[1]);
                    $lng = $temp[0];
                }

                $data_latlng = array(
                    'LAT' => $lng,
                    'LNG' => $lat
                );

                array_push($point_desa_list, $data_latlng);
            }

            $data_per_desa = array(
                'DESA_ID' => $r["DESA_ID"],
                'DESA_NAMA' => $r["DESA_NAMA"],
                'DESA_KODE' => $r["DESA_KODE"],
                'KEC_KODE' => $r["KEC_KODE"],
                'KECAMATAN_NAMA' => $r["KEC_NAMA"],
                'KAB_KODE' => $r["KAB_KODE"],
                'KAB_NAMA' => $r["KAB_NAMA"],
                'PROPINSI' => $r["PROV_KODE"],
                'DESA_POINT_LIST' => $point_desa_list,
                'COLOR' => $color
            );
            array_push($desa_list, $data_per_desa);
        }

        echo json_encode(array('Desa' => $desa_list));
        exit;
    }

    public function get_polygon_kec($id_kec = 0) {

        header('Access-Control-Allow-Origin: *');
        $kec_list = array();
        $p = $this->desa_model->get_kec_poly($id_kec);

        foreach ($p->result_array() as $r) {//data loc buat marker
            $data_way = explode(" ", $r["KEC_GEOMETRY"]);
            $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
            $color = '#' . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)];

            $point_kec_list = array();
            for ($i = 1; $i < count($data_way); $i++) {
                $latlng = explode(",", $data_way[$i]);
                $lat = $latlng[0];
                $lng = $latlng[1];

                if ($i == 0) {//echo $latlng[1];
                    $temp1 = explode(">", $latlng[0]);
                    $lat = $temp1[4];
                }

                if ($i == count($data_way) - 1) {//echo $latlng[1];
                    $temp = explode("<", $latlng[1]);
                    $lng = $temp[0];
                }

                $data_latlng = array(
                    'LAT' => $lng,
                    'LNG' => $lat
                );

                array_push($point_kec_list, $data_latlng);
            }

            $data_per_kec = array(
                'KEC_ID' => $r["KEC_ID"],
                'KEC_KODE' => $r["KEC_KODE"],
                'KEC_NAMA' => $r["KEC_NAMA"],
                'KAB_NAMA' => $r["KAB_NAMA"],
                'PROV_NAMA' => $r["PROV_NAMA"],
                'KEC_POINT_LIST' => $point_kec_list,
                'COLOR' => $color
            );
            array_push($kec_list, $data_per_kec);
        }

        echo json_encode(array('Desa' => $kec_list));
        exit;
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */